/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DialogCadFormaPagamento.java
 *
 * Created on 20/04/2011, 10:59:40
 */
package camada.apresentacao.dialog.orcamento;

import camada.bancodedados.daoimplemente.FormaPagamentoDAOImpl;
import eloma.classe.FormaPagamento;
import java.util.List;
import javax.swing.ListSelectionModel;
import tabelamodelcolumnmodel.orcamento.FormaPagamentoColumnModel;
import tabelamodelcolumnmodel.orcamento.FormaPagamentoTableModel;
import utilitarios.Exceptions;

/**
 *
 * @author Icaro
 */
public class DialogCadFormaPagamento extends javax.swing.JDialog {

    Exceptions exceptions = new Exceptions();
    FormaPagamento forma = new FormaPagamento();
    FormaPagamentoDAOImpl daoForma = new FormaPagamentoDAOImpl();
    String operacao = "salvar";

    /**
     * Creates new form DialogCadFormaPagamento
     */
    public DialogCadFormaPagamento(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setTitle("Cadastro forma pagamento");
        this.setLocationRelativeTo(null);
        configurarTabela();
    }

    public void configurarTabela() {
        try {

            this.setLista(daoForma.pesquisarTodosFormaPagamento());

            tabelaForma.setAutoCreateColumnsFromModel(false);
            java.awt.FontMetrics fm = tabelaForma.getFontMetrics(tabelaForma.getFont());

            tabelaForma.setColumnModel(new FormaPagamentoColumnModel(fm));
            tabelaForma.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        } catch (Exception e) {
            exceptions.Exceptions(e.getMessage(), "DialogCadFormaPagamento-configurarTabela");
        }
    }

    public void setLista(List<FormaPagamento> list) {
        this.tabelaForma.setModel(new FormaPagamentoTableModel(list));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btOk = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelaForma = new javax.swing.JTable();
        btRemover = new javax.swing.JButton();
        btCancelar = new javax.swing.JButton();
        btSalvar = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        btEditar = new javax.swing.JButton();
        btNovo = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txForma = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        btOk.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/outros/OK.png"))); // NOI18N
        btOk.setText("OK");
        btOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btOkActionPerformed(evt);
            }
        });

        tabelaForma.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tabelaForma.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelaFormaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabelaForma);

        btRemover.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Remove.png"))); // NOI18N
        btRemover.setText("Remover");
        btRemover.setEnabled(false);
        btRemover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRemoverActionPerformed(evt);
            }
        });

        btCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Cancel.png"))); // NOI18N
        btCancelar.setText("Cancelar");
        btCancelar.setEnabled(false);
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });

        btSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Save.png"))); // NOI18N
        btSalvar.setText("Salvar");
        btSalvar.setEnabled(false);
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Cancel.png"))); // NOI18N
        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        btEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Editar.png"))); // NOI18N
        btEditar.setText("Editar");
        btEditar.setEnabled(false);
        btEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditarActionPerformed(evt);
            }
        });

        btNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/cadastro/Add.png"))); // NOI18N
        btNovo.setText("Novo");
        btNovo.setToolTipText("Adicionar material");
        btNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados"));

        jLabel1.setText("Forma pagamento:");

        txForma.setEnabled(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(94, 94, 94)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(txForma, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(112, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txForma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(21, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 483, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(btOk, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(btNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btSalvar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btEditar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btRemover)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btCancelar)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 465, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btNovo)
                    .addComponent(btSalvar)
                    .addComponent(btEditar)
                    .addComponent(btRemover)
                    .addComponent(btCancelar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(btOk))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btOkActionPerformed
        this.hide();
}//GEN-LAST:event_btOkActionPerformed

    private void tabelaFormaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelaFormaMouseClicked
        try {

            txForma.setEnabled(false);

            btNovo.setEnabled(false);
            btSalvar.setEnabled(false);
            btEditar.setEnabled(true);
            btRemover.setEnabled(true);
            btCancelar.setEnabled(true);

            FormaPagamentoTableModel model = (FormaPagamentoTableModel) tabelaForma.getModel();
            this.forma = model.getValoresFormaPagamento(tabelaForma.getSelectedRow());

            txForma.setText(forma.getNomeForma());

        } catch (Exception e) {
            exceptions.Exceptions(e.getMessage(), "DialogCadFormaPagamento-tabelaFormaMouseClicked");
        }
}//GEN-LAST:event_tabelaFormaMouseClicked

    private void btRemoverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRemoverActionPerformed
        try {

            daoForma.removerFormaPagamento(forma.getIdFormaPagamento());

            txForma.setEnabled(false);
            txForma.setText("");

            btNovo.setEnabled(true);
            btSalvar.setEnabled(false);
            btEditar.setEnabled(false);
            btRemover.setEnabled(false);
            btCancelar.setEnabled(false);

            configurarTabela();

        } catch (Exception e) {
            exceptions.Exceptions(e.getMessage(), "DialogCadFormaPagamento-btRemoverActionPerformed");
        }
}//GEN-LAST:event_btRemoverActionPerformed

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
        txForma.setEnabled(false);

        btNovo.setEnabled(true);
        btSalvar.setEnabled(false);
        btEditar.setEnabled(false);
        btRemover.setEnabled(false);
        btCancelar.setEnabled(false);

        this.forma = null;
    }//GEN-LAST:event_btCancelarActionPerformed

    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed
        try {

            if (!operacao.equals("editar")) {

                FormaPagamento f = new FormaPagamento();
                f.setNomeForma(txForma.getText());

                daoForma.inserirFormaPagamento(f);

                configurarTabela();

                txForma.setText("");
                txForma.setEnabled(false);

            } else {
                forma.setNomeForma(txForma.getText());
                daoForma.atualizarFormaPagamento(forma);

                configurarTabela();

                txForma.setText("");
                txForma.setEnabled(false);
            }

        } catch (Exception e) {
            exceptions.Exceptions(e.getMessage(), "DialogCadFormaPagamento-btSalvarActionPerformed");
        }
}//GEN-LAST:event_btSalvarActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.hide();
}//GEN-LAST:event_jButton2ActionPerformed

    private void btEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditarActionPerformed
        txForma.setEnabled(true);

        btNovo.setEnabled(false);
        btSalvar.setEnabled(true);
        btEditar.setEnabled(false);
        btRemover.setEnabled(false);
        btCancelar.setEnabled(true);

        operacao = "editar";
}//GEN-LAST:event_btEditarActionPerformed

    private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoActionPerformed
        btNovo.setEnabled(false);
        btSalvar.setEnabled(true);
        btCancelar.setEnabled(true);

        txForma.setEnabled(true);
        txForma.setText(null);
}//GEN-LAST:event_btNovoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                DialogCadFormaPagamento dialog = new DialogCadFormaPagamento(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCancelar;
    private javax.swing.JButton btEditar;
    private javax.swing.JButton btNovo;
    private javax.swing.JButton btOk;
    private javax.swing.JButton btRemover;
    private javax.swing.JButton btSalvar;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabelaForma;
    private javax.swing.JTextField txForma;
    // End of variables declaration//GEN-END:variables

}
